﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SampleApplication.Models
{
    public interface IRepository<T, TKey>
        where T : class
    {
        T Find(TKey key);

        IQueryable<T> FindAll();

        void Insert(T target);

        void Delete(T target);
    }

    public abstract class RepositoryBase<T, TKey> : IRepository<T, TKey>
        where T : class
    {
        protected IUnitOfWork Unit { get; private set; }

        public RepositoryBase(IUnitOfWork unit = null)
        {
            this.Unit = unit ?? new SampleContext();
        }

        public T Find(TKey key)
        {
            return this.Unit.Set<T>().Find(key);
        }

        public IQueryable<T> FindAll()
        {
            return this.Unit.Set<T>();
        }

        public void Insert(T target)
        {
            this.Unit.Set<T>().Add(target);
        }

        public void Delete(T target)
        {
            this.Unit.Set<T>().Remove(target);
        }
    }
}
